package me.zhengjie.modules.deviceposition.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.devicemap.service.dto.DeviceMapDTO;
import me.zhengjie.modules.deviceposition.domain.DevicePosition;
import me.zhengjie.modules.deviceposition.service.DevicePositionService;
import me.zhengjie.modules.deviceposition.service.dto.DevicePositionDTO;
import me.zhengjie.modules.deviceposition.service.query.DevicePositionQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import me.zhengjie.modules.system.service.dto.DictDetailDTO;
import me.zhengjie.modules.system.service.query.DictDetailQueryService;
import me.zhengjie.utils.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @author 张鹏
 * @date 2019-06-05
 */
@RestController
@RequestMapping("api")
public class DevicePositionController {

    @Autowired
    private DevicePositionService devicePositionService;

    @Autowired
    private DevicePositionQueryService devicePositionQueryService;

    @Autowired
    private DictDetailQueryService dictDetailQueryService;

    private static final String ENTITY_NAME = "devicePosition";

    @Log("查询DevicePosition")
    @GetMapping(value = "/devicePosition")
    public ResponseEntity getDevicePositions(DevicePositionDTO resources, Pageable pageable) {
        return new ResponseEntity(devicePositionQueryService.queryAll(resources, pageable), HttpStatus.OK);
    }

    @Log("新增DevicePosition")
    @PostMapping(value = "/devicePosition")
    public ResponseEntity create(@Validated @RequestBody DevicePosition resources) {
        if (resources.getId() != null) {
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        UserDetails userDetails = SecurityContextHolder.getUserDetails();
        resources.setCreator(userDetails.getUsername());
        resources.setUpdator(userDetails.getUsername());
        Date now = new Date();
        resources.setCreateTime(new Timestamp(now.getTime()));
        resources.setUpdateTime(new Timestamp(now.getTime()));
        return new ResponseEntity(devicePositionService.create(resources), HttpStatus.CREATED);
    }

    @Log("修改DevicePosition")
    @PutMapping(value = "/devicePosition")
    public ResponseEntity update(@Validated @RequestBody DevicePosition resources) {
        if (resources.getId() == null) {
            throw new BadRequestException(ENTITY_NAME + " ID Can not be empty");
        }
        UserDetails userDetails = SecurityContextHolder.getUserDetails();
        resources.setUpdator(userDetails.getUsername());
        Date now = new Date();
        resources.setUpdateTime(new Timestamp(now.getTime()));
        devicePositionService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除DevicePosition")
    @DeleteMapping(value = "/devicePosition/{id}")
    public ResponseEntity delete(@PathVariable Long id) {
        devicePositionService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("获取AccessDevice所需字典")
    @GetMapping(value = "/devicePosition/dict")
    public ResponseEntity getDict() {
        Map<String, Object> map = new HashMap<>();
        map.put("device", devicePositionService.deviceDict());
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @Log("查询DevicePosition列表")
    @GetMapping(value = "/devicePosition/list/{id}")
    public ResponseEntity getList(@PathVariable Long id) {
        DevicePositionDTO resources = new DevicePositionDTO();
        resources.setMapId(id);
        return new ResponseEntity(devicePositionQueryService.queryAll(resources), HttpStatus.OK);
    }
}